package com.gtw.modules.sys.controller;

import com.gtw.common.annotation.LoginUserSys;
import com.gtw.common.annotation.SysLog;
import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.entity.*;
import com.gtw.modules.sys.service.SysCommodityService;
import com.gtw.modules.sys.service.SysGroupbuyService;
import com.gtw.modules.sys.service.TbGroupbuCommodityService;
import com.gtw.utils.UidUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/** 团购controller
 *
 * @author wuye
 * @date 2018/2/24
 */
@RestController
@RequestMapping("/sys/groupbuy")
public class SysGroupBuyController extends AbstractController{

    @Autowired
    private SysGroupbuyService sysGroupbuyService;

    @Autowired
    private SysCommodityService sysCommodityService;

    @Autowired
    private TbGroupbuCommodityService tbGroupbuCommodityService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:groupbuy:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);

        List<TbGroupbuy> tbGroupbuyList = sysGroupbuyService.queryList(query);
        int total = sysGroupbuyService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(tbGroupbuyList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 根据团购ID查询商品列表
     * @return
     */
    @RequestMapping("/foundCommodity")
    public R foundCommodity(@RequestParam Map<String, Object> params,@RequestParam Integer groupbuyid){
        params.put("groupbuyid",groupbuyid);
        //查询列表数据
        Query query = new Query(params);
        List<TbGroupbuCommodity> tbGroupbuCommodityList = tbGroupbuCommodityService.queryList(query);
        List<TbCommodity> commodityList = new ArrayList<>();
        for (TbGroupbuCommodity tgc: tbGroupbuCommodityList) {
            TbCommodity tbCommodity = sysCommodityService.queryObject(tgc.getCommodityid());
            commodityList.add(tbCommodity);
        }
        int total = tbGroupbuCommodityService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(commodityList, total, query.getLimit(), query.getPage());

        return R.ok().put("data", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:groupbuy:info")
    public R info(@PathVariable("id") Integer id) throws ParseException {
        TbGroupbuy tbGroupbuy = sysGroupbuyService.queryObject(id);
        tbGroupbuy.setCurrentprice(tbGroupbuy.getCurrentprice()/100);
        tbGroupbuy.setMinprice(tbGroupbuy.getMinprice()/100);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String str1 = sdf.format(tbGroupbuy.getStarttime());
//        String str2 = sdf.format(tbGroupbuy.getEndtime());
//        Date starttime = sdf.parse(str1);
//        Date enttime = sdf.parse(str2);
//        tbGroupbuy.setStarttime(starttime);
//        tbGroupbuy.setEndtime(enttime);
        return R.ok().put("tbGroupbuy", tbGroupbuy);
    }

    /**
     * 保存
     */
    @SysLog("添加团购信息")
    @RequestMapping("/save")
    @RequiresPermissions("sys:groupbuy:save")
    public R save(@RequestBody TbGroupbuy tbGroupbuy){
        TbCommodity tbCommodity = sysCommodityService.queryObject(tbGroupbuy.getCommodityid());
        tbGroupbuy.setOldprice(tbCommodity.getPrice());
        tbGroupbuy.setUid(UidUtil.getUUID());
        tbGroupbuy.setCreatetime(new Date());
        tbGroupbuy.setCreateuser(getUser().getUsername());
        tbGroupbuy.setIsopen(0);
        tbGroupbuy.setIsdelete(0);
        tbGroupbuy.setIsstart(0);
        tbGroupbuy.setPepnum(0);
        tbGroupbuy.setRestrict(7);
        tbGroupbuy.setCurrentprice(tbGroupbuy.getCurrentprice()*100);
        tbGroupbuy.setOldprice(tbCommodity.getPrice());
        tbGroupbuy.setMinprice(tbGroupbuy.getMinprice()*100);
        sysGroupbuyService.save(tbGroupbuy);
        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改团购信息")
    @RequestMapping("/update")
    @RequiresPermissions("sys:groupbuy:update")
    public R update(@RequestBody TbGroupbuy tbGroupbuy){
        TbCommodity tbCommodity = sysCommodityService.queryObject(tbGroupbuy.getCommodityid());
        tbGroupbuy.setCurrentprice(tbGroupbuy.getCurrentprice()*100);
        tbGroupbuy.setOldprice(tbCommodity.getPrice());
        tbGroupbuy.setMinprice(tbGroupbuy.getMinprice()*100);
        sysGroupbuyService.update(tbGroupbuy);
        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除团购信息")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:groupbuy:delete")
    public R delete(@RequestBody Integer[] ids){
        for (int i = 0; i<ids.length;i++){
            int id = ids[i];
            TbGroupbuy tbGroupbuy = sysGroupbuyService.queryObject(id);
            tbGroupbuy.setIsdelete(1);
            sysGroupbuyService.update(tbGroupbuy);
        }

        return R.ok();
    }

}
